using System;
using System.Text;
using System.Threading;
namespace Leet
{
    class P001
    {
        public static void Run()
        {
            Profiler.Run(Solution.RevertList, CreateInput(new int[] { }), ShowResult);
            Profiler.Run(Solution.RevertList, CreateInput(new int[] { 1, 2,  }), ShowResult);
            Profiler.Run(Solution.RevertList, CreateInput(new int[] { 1, 2, 3, }), ShowResult);
            Profiler.Run(Solution.RevertList, CreateInput(new int[] { 1, 2, 3, 4, 5 }), ShowResult);
            Profiler.Run(Solution.RevertList, CreateInput(new int[] { 1, }), ShowResult);
            Profiler.Run(Solution.RevertList, CreateInput(new int[] { }), ShowResult);
        }

        static ListNode CreateInput(int[] arr)
        {
            ListNode firstNode = null;
            ListNode lastNode = null;
            foreach (var v in arr)
            {
                var node = new ListNode() { value = v };
                if (firstNode == null)
                    firstNode = node;
                if (lastNode != null)
                    lastNode.next = node;
                lastNode = node;
            }

            return firstNode;
        }

        static void ShowResult(ListNode node)
        {
            var sb = new StringBuilder();
            while (node != null)
            {
                sb.Append(node.value + " ");
                node = node.next;
            }

            Console.WriteLine(sb);
        }

        class ListNode
        {
            public int value;
            public ListNode next;
        }
        class Solution
        {
            //掐节算法
            //每次循环时 将头节点掐下来 指向新的链表的头节点 node.next = newHeadNode
            //新链表的头节点指针也就是 node.next = preNode
            //在把头节点掐下来之前,先用另外一个变量记录一下,以免指针丢失  nextNode = node.next;
            public static ListNode RevertList(ListNode node)
            {
                ListNode preNode = null;
                while (node != null)
                {
                    var t = node.next;
                    node.next = preNode;

                    preNode = node;
                    node = t;
                }
                return preNode;
            }
        }
    }
}